﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：徐彦龙 
    //
    // 文件名：RoleList.cs
    // 文件功能描述：角色管理：提供新增用户、修改用户、删除用户
    // 创建标识：徐彦龙2009-03-06
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/

using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;

using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin
{
    public partial class RoleList : BasePage
    {
        public RoleList()
        {
            this.ModuleNo = Module.系统管理.ToString();
            this.PageOperateType = OperateType.主用户管理.ToString();
        }

        IBLL_Role bl = Factory.BLL_Role();
        protected void Page_Load(object sender, EventArgs e)
        {
            //非超级管理员不允许操作此页面
            if (!CurrentAccount.IsSuperAdmin())
            {
                PageError("非超级管理员不允许操作此功能", "");
            }

            if (!this.IsPostBack)
            {
                this.SetBind();
            }
        }

        /// <summary>
        /// 绑定角色列表
        /// </summary>
        private void SetBind()
        {
            IList<Role> lr = bl.GetAllRole();
            this.gvwRoleList.DataSource = lr;
            this.gvwRoleList.DataBind();
        }

        /// <summary>
        /// 新增角色及权限,转向新页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("RoleAdd.aspx");
        }

        /// <summary>
        /// 根据角色编号删除角色及相应的权限信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwRoleList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Del"))
            {
                string strRoleID = e.CommandArgument.ToString();
                IBLL_Role bl = Factory.BLL_Role();
                int intRtn = bl.DeleteRole(strRoleID);
                if (intRtn == 0)
                {
                    PageError("删除失败！","");
                }
                if (intRtn == 1)
                {
                    this.SetBind();
                    PageInfo("删除成功！","");
                }
                if (intRtn == 2)
                {
                    PageError("该角色不存在，不能删除！","");
                }
                if (intRtn == 3)
                {
                    PageError("该角色已经被使用，不能删除！","");
                }

            }
        }


        /// <summary>
        /// 设定操作是否可见
        /// </summary>
        /// <param name="objIsDefault"></param>
        /// <returns></returns>
        public bool delVisible(object objIsDefault)
        {
            bool IsDefault = false;
            if (objIsDefault != null)
            {
                IsDefault = (bool)objIsDefault;
            }
            return !IsDefault;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwRoleList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvwRoleList.EditIndex = e.NewEditIndex;
            SetBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwRoleList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Role role = new Role();
            role.RoleID = Convert.ToString(gvwRoleList.DataKeys[e.RowIndex].Value);
            if (!string.IsNullOrEmpty(((TextBox)gvwRoleList.Rows[e.RowIndex].FindControl("txtRoleName")).Text.Trim()))
            {
                role.RoleName = ((TextBox)gvwRoleList.Rows[e.RowIndex].FindControl("txtRoleName")).Text.Trim();
            }
            else
            {
                HZ.Web.WebUtil.Alert("角色名称不能为空！",this);
                ((TextBox)gvwRoleList.Rows[e.RowIndex].FindControl("txtRoleName")).Focus();
                return;
            }
           
            int flag = bl.SetRole(role);
            switch (flag)
            {
                case 0:
                    PageError("更新失败！", "");
                    break;
                case 1:
                    //PageInfo("更新成功！", "");
                    gvwRoleList.EditIndex = -1;
                    this.SetBind();
                    break;
                case 2:
                    PageError("该角色名称已经存在！", "");
                    break;
                case 3:
                    PageError("该角色已经不存在！", "");
                    break;
                default:
                    break;
            }


        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwRoleList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvwRoleList.EditIndex = -1;
            SetBind();
        }
    }
}
